package others;

import com.google.gson.GsonBuilder;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.security.cert.X509Certificate;

public class ParseQg {

    public static void main(String[] args) throws Exception {
        try (FileWriter fileWriter = new FileWriter("D:\\data\\govcg\\ins.sql")){
            int start = 1;
            for (int i = 1; i <= 13;i ++) {
                fileWriter.write(parseHtml(i, 2023, start));
            }
            for (int i = 1; i <= 20; i++) {
                fileWriter.write(parseHtml(i, 2024, start + 260));
            }
            fileWriter.flush();
        }

    }

    public static String parseHtml(int page, int year, int start) throws IOException {
        Document document = Jsoup
                .parse(new File("D:\\data\\govcg\\ch"+year +"_"+page+".html"));
        Elements links = document.body()
                .select(".vT-srch-result-list-bid li");
        String sqlTemp = "insert into qgxx(title, url, description, type,zbje,zbdw, pubtime) value ('%s','%s','%s','%s','%s','%s','%s')";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < links.size(); i++) {
            try {
                Element element = links.get(i);
                Elements a = element.select("a");
                String link = a.attr("href");
                String type = element.select("strong").text();
                String href = a.attr("href");
                String title = a.text();
                String description = element.select("p").text();
                String pubTime = element.select("span").text();
                String[] split = pubTime.split("\\|");
                pubTime = split[0];
//                sb.append(String.format(sqlTemp, trim, start + i + (page - 1) * 20));
                if (type.startsWith("公开招标公告")) {
                    Document document1 = Jsoup.connect(link)
                            .get();
//                    Elements pubTime = document1.body().select("#pubTime");
                    Elements contentList = document1.select(".vF_detail_content p");
                    String ysjz = "";
                    for (int j = 0; j < contentList.size(); j++) {
                        String text = contentList.get(j).text();
                        if (text.startsWith("预算金额：")) {
//                            sb.append(String.format(sqlTemp, text.replace("预算金额：", ""), start + i + (page - 1) * 20));
                            ysjz = text;
                            break;
                        }
                    }
                    sb.append(String.format(sqlTemp, title, href, description, type, ysjz,"", pubTime));

                } else if (type.startsWith("中标公告") || type.startsWith("成交公告")) {
                    Document document1 = Jsoup.connect(link).get();
                    Elements bidResult = document1.select("#_notice_content_noticeBidResult-noticeBidResult");
                    Elements select = bidResult.select("tr td");
                    String zbje = "";
                    String zbdw = "";
                    if (select.size() >= 2) {
                        zbje = select.get(2).text();
                        zbdw = select.get(0).text();
                    }
                    sb.append(String.format(sqlTemp, title, href, description, type, zbje,zbdw, pubTime));
                } else {
                    sb.append(String.format(sqlTemp, title, href, description, type, "","", pubTime));
                }

                System.out.println("完成了 " + (start + i + (page - 1) * 20));
            } catch (Exception e) {
                System.out.println("错误 " + (start + i + (page - 1) * 20));
                e.printStackTrace();
            }

        }
        return sb.toString();
    }

}
